<?php
/**
 * KayPaste
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category   KayPaste
 * @package    Paste
 * @subpackage Forms
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */

/**
 * Imports
 */
require_once 'Zend/Controller/Action/HelperBroker.php';
require_once 'Zend/Form.php';
require_once 'Zend/Form/Element/Hidden.php';
require_once 'Zend/Form/Element/Password.php';
require_once 'Zend/Form/Element/Submit.php';
require_once 'Zend/Form/Element/Select.php';
require_once 'Zend/Form/Element/Text.php';
require_once 'Zend/Form/Element/Textarea.php';

/**
 * Paste_Form_NewPaste
 * 
 * Form for creating a new paste.
 *
 * @category   KayPaste
 * @package    Paste
 * @subpackage Forms
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */
class Paste_Form_NewPaste extends Zend_Form
{
    /**
     * Initializes form
     * 
     */
    public function init()
    {
        // default url to post to
        $url = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
        $url = $url->url(
            array(
                'module'     => 'paste',
                'controller' => 'index',
                'action'     => 'process'
            )
        );
        
        // set form attribs
        $this->setMethod('post')
             ->setAction($url)
             ->setAttrib('class', 'paste');
        
        // get highlighter for types
        $highlighter = new Zym_Highlight_Adapter_Colorer();
        
        // create elements
        $type        = new Zend_Form_Element_Select('pasteType');
        $author      = new Zend_Form_Element_Text('author');
        $hidden      = new Zend_Form_Element_Hidden('confirm');
        $description = new Zend_Form_Element_Text('description');
        $password    = new Zend_Form_Element_Password('paste-pass');
        $paste       = new Zend_Form_Element_Textarea('paste');
        $submit      = new Zend_Form_Element_Submit('submit');
        
        // add elements to form
        $this->addElement($type)
             ->addElement($author)
             ->addElement($hidden)
             ->addElement($description)
             ->addElement($password)
             ->addElement($paste)
             ->addElement($submit);
        
        // setup type element
        $type->setAllowEmpty(false)
             ->setRequired(false)
             ->setAttrib('id', 'paste-type')
             ->setLabel('Type')
             ->setDisableTranslator(true)
             ->addValidators(array(
                array('NotEmpty', true)
             ));
        
        foreach ($highlighter->getTypes() as $cat => $types) {
            $type->addMultiOption($cat, $types);
        }
        
        // setup author element
        $author->setAllowEmpty(true)
               ->setRequired(false)
               ->setAttrib('id', 'paste-author')
               ->setAttrib('size', 64)
               ->setAttrib('max-length', 64)
               ->setAttrib('class', 'text')
               ->setLabel('Your name')
               ->setOptions(array('escape' => null))
               ->addValidators(array(
                   array('NotEmpty', true),
                   array('StringLength', true, array(0, 64)),
               ));
        
        // setup hidden element
        $hidden->setAllowEmpty(true)
               ->setRequired(false)
               ->addValidator('StringLength', true, array(0, 0));
        
        // setup description element
        $description->setAllowEmpty(true)
                    ->setRequired(false)
                    ->setAttrib('id', 'paste-description')
                    ->setAttrib('size', 64)
                    ->setAttrib('max-length', 512)
                    ->setAttrib('class', 'text')
                    ->setLabel('Description')
                    ->addValidators(array(
                        array('NotEmpty', true),
                        array('StringLength', true, array(0, 512)),
                    ));
        
        // setup password element
        $password->setAllowEmpty(true)
                 ->setRequired(false)
                 ->setAttrib('id', 'paste-password')
                 ->setAttrib('size', 64)
                 ->setAttrib('max-length', 64)
                 ->setAttrib('class', 'text')
                 ->setLabel('Password')
                 ->addValidators(array(
                     array('NotEmpty', true),
                     array('StringLength', true, array(2, 64)),
                 ));
        
        // setup paste element
        $paste->setAllowEmpty(false)
              ->setRequired(true)
              ->setAttrib('id', 'paste-paste')
              ->setAttrib('cols', 120)
              ->setAttrib('rows', 40)
              ->setLabel('Paste')
              ->addValidators(array(
                  array('NotEmpty', true)
              ));
        
        // setup submit button
        $submit->setLabel('Paste away!')
               ->setAttrib('id', 'paste-submit')
               ->setAttrib('class', 'button');
    }
}
